#include "expsamplelistframe.h"
#include "ui_expsamplelistframe.h"
#include "sourcemanage.h"
#include "widgetmanage.h"
#include "delegate.h"

#include "mainframe.h"
#include "sequencerunframe.h"
#include "inputcndialog.h"

ExpSampleListFrame::ExpSampleListFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpSampleListFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;
    pExpSample=pExperiment->GetExpSamplePtr();

    ComboxDelegate *pComboxDelegate=new ComboxDelegate(this,6);
    ui->tableWidget->setItemDelegateForColumn(2,pComboxDelegate);
    pComboxDelegate=new ComboxDelegate(this,6);
    ui->tableWidget->setItemDelegateForColumn(3,pComboxDelegate);
    CheckBox *pCheckBox=new CheckBox(this,0);
    ui->tableWidget->setItemDelegateForColumn(11,pCheckBox);

    SequenceRunFrame *pSequenceRunFrame=(SequenceRunFrame*)WM->GetWidget("SequenceRunFrame");
    MainFrame *pMainFrame=(MainFrame*)WM->GetWidget("MainFrame");
    connect(this,SIGNAL(SIG_SaveClicked()),pMainFrame,SLOT(on_toolButton_SaveExp_clicked()));
    connect(this,SIGNAL(SIG_DataChanged()),pSequenceRunFrame,SLOT(on_toolButton_Update_clicked()));
}

ExpSampleListFrame::~ExpSampleListFrame()
{
    delete ui;
}

void ExpSampleListFrame::LoadData()
{
    if(pExperiment==NULL) return;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();

    disconnect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_itemChanged(QTableWidgetItem*)));
    int count=sampleInfors.count();
    ui->tableWidget->setRowCount(count);
    for(int row=0;row<count;row++)
    {
        int colomn=0;
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(QString::number(row));
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).sampleName);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).sampleType);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).qcType);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).restartSample);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).relateSample);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sampleInfors.at(row).preScanCount));
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sampleInfors.at(row).mainScanCount));
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sampleInfors.at(row).dilutionFactor));
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).internalS);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).specialBlank);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(sampleInfors.at(row).bShowReport));//
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(sampleInfors.at(row).samplePositon);
        ui->tableWidget->setItem(row,colomn++,pItem);
    }
    connect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_itemChanged(QTableWidgetItem*)));
}

void ExpSampleListFrame::on_pushButton_Add_clicked()
{
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> *pSampleInfors=pExpSample->GetSampleInforsPtr();
    SampleInfor sampleInfor;

    QModelIndex index=ui->tableWidget->currentIndex();
    //qDebug()<<"index.row()"<<index.row();
    pSampleInfors->insert(index.row()+1,sampleInfor);

    LoadData();
   // emit SIG_DataChanged();
}

void ExpSampleListFrame::on_pushButton_Del_clicked()
{
    QModelIndex index=ui->tableWidget->currentIndex();
    if(index.isValid()==false) return ;

    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> *pSampleInfors=pExpSample->GetSampleInforsPtr();
    pSampleInfors->removeAt(index.row());

    LoadData();
    //emit SIG_DataChanged();
}

void ExpSampleListFrame::on_pushButton_Input_clicked()
{
    InputCnDialog inputCnDialog(pExperiment);
    inputCnDialog.exec();
}

void ExpSampleListFrame::on_pushButton_Save_clicked()
{
    emit SIG_SaveClicked();
}

void ExpSampleListFrame::on_pushButton_AddToSeq_clicked()
{
    emit SIG_SaveClicked();//success

    QueueExp *queueExp=SM->GetQueueExp();
    queueExp->AddExp(pExperiment->GetFilePath(),*pExperiment);

    emit SIG_DataChanged();
}

void ExpSampleListFrame::on_tableWidget_itemChanged(QTableWidgetItem *item)
{
    //check
    int row=item->row();
    int column=1;
    SampleInfor sampleInfor=pExpSample->GetSampleInfor(row);
    QString text;

    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.sampleName=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.sampleType=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.qcType=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.restartSample=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.relateSample=text;
    text=ui->tableWidget->item(row,column++)->text();
    if(column==6)
    {
        sampleInfor.SetPreScanCount(text.toInt());//
    }
    text=ui->tableWidget->item(row,column++)->text();
    if(column==7)
    {
        sampleInfor.SetMainScanCount(text.toInt());//
    }
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.dilutionFactor=text.toDouble();
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.internalS=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.specialBlank=text;
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.bShowReport=text.toInt();
    text=ui->tableWidget->item(row,column++)->text();
    sampleInfor.samplePositon=text;

    pExpSample->SetSampleInfor(row,sampleInfor);

    //emit SIG_DataChanged();
}
